import { contextBridge, ipcRenderer, IpcRendererEvent, shell } from "electron";
import { platform, release, arch } from "os";
import { onUnmounted } from "vue";
import { IpcChannelMainClass, IpcChannelRendererClass } from "../ipc/index";

function getIpcRenderer() {
  const IpcRenderer = {};
  Object.keys(new IpcChannelMainClass()).forEach((channel) => {
    IpcRenderer[channel] = {
      invoke: async (args: any) => ipcRenderer.invoke(channel, args),
    };
  });
  Object.keys(new IpcChannelRendererClass()).forEach((channel) => {
    IpcRenderer[channel] = {
      on: (listener: (...args: any[]) => void) => {
        ipcRenderer.on(channel, listener);
        onUnmounted(() => {
          ipcRenderer.removeListener(channel, listener);
        });
      },
      once: (listener: (...args: any[]) => void) => {
        ipcRenderer.once(channel, listener);
        onUnmounted(() => {
          ipcRenderer.removeListener(channel, listener);
        });
      },
      removeAllListeners: () => ipcRenderer.removeAllListeners(channel),
    };
  });
  return IpcRenderer;
}

contextBridge.exposeInMainWorld("ipcRendererChannel", getIpcRenderer());

contextBridge.exposeInMainWorld("systemInfo", {
  platform: platform(),
  release: release(),
  arch: arch(),
});

contextBridge.exposeInMainWorld("shell", shell);
contextBridge.exposeInMainWorld("crash", {
  start: () => {
    process.crash();
  },
});

contextBridge.exposeInMainWorld("ipcFn", {
  // 选择文件夹
  selectFolders: () =>
    ipcRenderer.invoke("select-folders") as Promise<string[] | null>,
  //调用控制台
  executeCommand: (
    folderPath: string, // 文件夹路径
    command: string, // 命令
    useCmd: boolean = true // 是否使用cmd
  ) => ipcRenderer.invoke("execute-command", folderPath, command, useCmd),
  readAllFolders: (
    dirPath: string // 文件夹路径
  ) => ipcRenderer.invoke("read-all-folders", dirPath),
  openUrl: (url: string) => ipcRenderer.invoke("open-url", url),
  getNodeList: (useExec?: boolean) =>
    ipcRenderer.invoke("get-node-list", useExec),
  selectFiles: () => ipcRenderer.invoke("select-files"),
  launchFiles: (selectedPaths: string[]) =>
    ipcRenderer.invoke("launch-files", selectedPaths),
  batchRenameFileIncrement: (
    batchRename: string,
    batchRenameRule: string,
    replaceExt?: boolean
  ) =>
    ipcRenderer.invoke(
      "batchRenameFileIncrement",
      batchRename,
      batchRenameRule,
      replaceExt
    ),
  batchRenameFileReplace: (
    batchRename: string,
    batchRenameRule: string,
    replaceExt?: boolean
  ) =>
    ipcRenderer.invoke(
      "batchRenameFileReplace",
      batchRename,
      batchRenameRule,
      replaceExt
    ),
  // installDependencies: (folderPath: string, packageManager: string) =>
  //   ipcRenderer.invoke("install-dependencies", folderPath, packageManager),
  // startProject: (folderPath: string, packageManager: string, command: string) =>
  //   ipcRenderer.invoke("start-project", folderPath, packageManager, command),
  // buildProject: (folderPath: string, packageManager: string, command: string) =>
  //   ipcRenderer.invoke("build-project", folderPath, packageManager, command),
});
